package com.icoolkj.mall.order01.controller;

import com.icoolkj.mall.common.Result;
import com.icoolkj.mall.order01.dto.OrderRequest;
import com.icoolkj.mall.order01.dto.OrderResponse;
import com.icoolkj.mall.order01.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/16 14:41
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/getOrderByUserId")
    public Result<List<OrderResponse>> getOrderByUserId(@RequestParam("userId") Long userId){
        log.info("根据用户ID：{} 查询订单信息", userId);
        try {
            //Thread.sleep(5000);
           return orderService.getOrderByUserId(userId);
        } catch (RuntimeException e) {
            return Result.fail(e.getMessage());
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
        }
    }

    @PostMapping("/post1")
    public Result<OrderResponse> post1(@RequestBody OrderRequest orderRequest){
        log.info("orderRequest ==> {}" , orderRequest);
        OrderResponse orderResponse = new OrderResponse();
        orderResponse.setOrderId(1L);
        orderResponse.setOrderQuantity(orderRequest.getOrderQuantity());
        orderResponse.setUserId(orderRequest.getUserId());
        orderResponse.setProductId(orderRequest.getProductId());
        orderResponse.setOrderCost(new BigDecimal(12588.88));
        return Result.ok(orderResponse);
    }

    @PostMapping("/post2")
    public Result<OrderResponse> post2(@RequestBody OrderRequest orderRequest, @RequestParam("token") String token){
        log.info("token ==> {}" , token);
        OrderResponse orderResponse = new OrderResponse();
        orderResponse.setOrderId(1L);
        orderResponse.setOrderQuantity(orderRequest.getOrderQuantity());
        orderResponse.setUserId(orderRequest.getUserId());
        orderResponse.setProductId(orderRequest.getProductId());
        orderResponse.setOrderCost(new BigDecimal(12588.88));
        return Result.ok(orderResponse);
    }

    @PostMapping("/post3/{userId}")
    public Result<OrderResponse> post3(@RequestBody OrderRequest orderRequest, @PathVariable("userId") Long userId){
        log.info("userId ==> {}" , userId);
        OrderResponse orderResponse = new OrderResponse();
        orderResponse.setOrderId(1L);
        orderResponse.setOrderQuantity(orderRequest.getOrderQuantity());
        orderResponse.setUserId(orderRequest.getUserId());
        orderResponse.setProductId(orderRequest.getProductId());
        orderResponse.setOrderCost(new BigDecimal(12588.88));
        return Result.ok(orderResponse);
    }

}
